package com.maybach.core.generator;

import com.maybach.core.enums.NumEnum;
import com.maybach.core.manager.IdentifyManager;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by houenxun on 16/2/24.
 * 编号生成器
 */
@Component
public class NumGenerator {

    public static long MAX_VALUE =1000000L;

    public static int MAX_LENGTH =6;


    @Autowired
    private
     IdentifyManager identifyManager;
    /**
     * 生成编号
     * @param type
     * @return
     */
    public String generate(NumEnum type){
        return this.generate(type, this.getLongNum(type));
    }

    public Long getLongNum(NumEnum type){
        return identifyManager.createAndGetIdentify(type.getCode());
    }

    public String generate(NumEnum type, Long value){
        StringBuilder builder = new StringBuilder();
        if(StringUtils.isNotBlank(type.getPrefix())){
            builder.append(type.getPrefix());
        }
        DateFormat format = new SimpleDateFormat("YYMMdd");
        builder.append(format.format(new Date()));
        String temp = "00000" + value;
        builder.append(temp.substring(temp.length() - MAX_LENGTH, temp.length()));

        if(StringUtils.isNotBlank(type.getPostfix())){
            builder.append(type.getPostfix());
        }

        return builder.toString();
    }




    }
